#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : lee

import sys
import os
import logging
import re
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CfxGrowMeshAttrsCheck(CheckBase):
    def __init__(self):
        super(CfxGrowMeshAttrsCheck, self).__init__()

    def check_grow_mesh_attrs(self):
        """
        描述：pgYeti节点检查的启动函数
        """
        # 通过文件名称获取资产信息
        maya_file = mu.sceneName()
        file_name = os.path.basename(maya_file)
        asset_short_type = file_name.split("_")[0]
        asset_name = file_name.split("_")[1]

        miss_node = []
        growMesh_group = "{0}_{1}_growMesh_grp".format(asset_short_type, asset_name)
        for node in cmds.listRelatives(growMesh_group, c=1):
            shape_node = cmds.listRelatives(node, c=1)[0]
            # 检查yeti生长面细分是否开启，默认应为开启
            if not cmds.getAttr(shape_node + ".yetiSubdivision"):
                miss_node.append(node)

        return miss_node

    def check(self):
        result_proc = self.check_grow_mesh_attrs()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        try:
            result_proc = self.check_grow_mesh_attrs()
            for node in result_proc:
                shape_node = cmds.listRelatives(node, c=1)[0]
                cmds.setAttr(shape_node + ".yetiSubdivision", True)
            result_log = u'Succeed:没有问题\n'
            return result_log
        except:
            result_proc = self.check_grow_mesh_attrs()
            result_log = u'Error:%s\n修复失败' % result_proc
